<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Vote\Model;

use Think\Model;

class AppapplyModel extends Model {

    /**
     * 
     * @param type $openid
     * @return boolean
     * 是否参与
     */
    function checkApply($openid) {
        $json = array('status' => 0);
        $where['openid'] = $openid;
        $rs = $this->where($where)->find();
        if ($rs) {
            return $rs;
        } else {
            return false;
        }
    }

    /**
     * 
     * @param type $cardNo
     * @param type $data
     * 插入数据
     */
    function insert($cardNo, $data = array()) {

        $json = array('status' => 0);
        $openid = $data['openid'];
        $rs = $this->checkApply($openid); //检测是否已经参与
        $huiyuan = check_vip($openid); //会员检测
        if ($huiyuan) {
            if (!$rs) {
                $where['app_name'] = $data['app_name'];
                $app_name = $this->where($where)->find();
                if ($app_name) {
                    $json['info'] = "app名称已存在~";
                } else {
                    $state = $this->add($data);
                    if ($state) {
                        addCredits($cardNo, 1000); //增及积分
                        $scorearr = array(
                            'qid' => 'appapply',
                            'orderid' => $state,
                            'score' => 1000,
                            'huiyuanopenid' => $data['openid'],
                            'huiyuantel' => $data['huiyuan_tel'],
                            'ctime' => getSysTime(),
                        );
                        M('score_scorelog')->add($scorearr); //记录扣除积分日志
                        $json['status'] = 1;
                        $json['id'] = $state;
                        $json['info'] = "您的提交成功啦！您已获1000积分！12月25日万元现金花落谁家？敬请期待！";
                    } else {
                        $json['info'] = "出现异常";
                    }
                }
            } else {
                $json['info'] = "你已经参与过~";
            }
        } else {
            $redirec_url = "http://ldh.mo2.cn/vote.php/Appapply/loadInit";
            $url = checkFans($openid, $redirec_url, 1);
            $json['status'] = 2;
            $json['url'] = $url;
        }
        echo json_encode($json);
    }

    /**
     * 
     * @param type $id
     * @return string
     * 查询报名信息
     */
    function getApplyInfById($id) {
        if ($id) {
            $rs = $this->where('id=' . $id)->find();
            if ($rs) {
                return $rs;
            } else {
                return "信息查询失败";
            }
        } else {
            return "信息查询失败";
        }
    }

}
